package kr.hkit.androidcomplete03.ch20;

import kr.hkit.androidcomplete03.R;
import kr.hkit.androidcomplete03.ch20.FragmentEx.CounterFragment;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class FragmentManagerEx extends Activity {
	FragmentManager fm;
	Fragment fragment;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch20_fragmentmanagerex);
	}
	
	public void mOnClick(View v){
		fm = getFragmentManager();
		fragment = fm.findFragmentById(R.id.ch20_fragmentmanager_frame);
		
		switch(v.getId()){
		case R.id.ch20_fragmentmanager_add:
			if(fragment == null){
				FragmentTransaction ftr = fm.beginTransaction();
				CounterFragment countF = new CounterFragment();
				ftr.add(R.id.ch20_fragmentmanager_frame, countF, "counter");
				ftr.commit();
			}else{
				Toast.makeText(this, "이미 추가 되어 있슴둥", 0).show();
			}
			break;
		case R.id.ch20_fragmentmanager_remove:
			if(fragment == null){
				Toast.makeText(this, "프래그먼트가 없슴둥", 0).show();
			}else{
				FragmentTransaction ftr = fm.beginTransaction();
				ftr.remove(fragment);
				ftr.commit();
			}
			break;
		case R.id.ch20_fragmentmanager_hide:
			if(fragment == null){
				Toast.makeText(this, "프로그먼트가 없슴둥", 0).show();
			}else{
				FragmentTransaction ftr = fm.beginTransaction();
				if(fragment.isHidden()){
					ftr.show(fragment);
				}else{
					ftr.hide(fragment);
				}
				ftr.commit();
			}
			break;
		case R.id.ch20_fragmentmanager_replace:
			if(fragment == null){
				Toast.makeText(this, "프로그먼트가 없슴둥", 0).show();
			}else{
				FragmentTransaction ftr = fm.beginTransaction();
				if(fragment.getTag() == "counter"){
					TextFragmentEx tf = new TextFragmentEx();
					ftr.replace(R.id.ch20_fragmentmanager_frame, tf,"text");
				}else{
					CounterFragment cf = new CounterFragment();
					ftr.replace(R.id.ch20_fragmentmanager_frame, cf,"counter");
				}
				ftr.commit();
			}
			break;
		}
	}
}
